
package com.wdf.dota;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wdf.bean.Comment;
import com.wdf.bean.PostingDiscussion;
import com.wdf.rest.service.DotaDiscussionRestClient;

public class SubmitPostingDiscussionActivity  extends Activity{
	
	Button returnCommentBtn;
	Button submitCommentBtn;
	
	TextView myUsernameView ;
	
	EditText myComment;
	
	DotaDiscussionRestService service  ;
	String topic;
	String username;
	
	String postingId;
    String accountId;

	
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.submit_postingdiscussion);
		submitCommentBtn =  (Button) findViewById(R.id.submit_comment);
		submitCommentBtn.setOnClickListener(submitCommentButtonListener);
		
		returnCommentBtn =  (Button) findViewById(R.id.return_comment);
		returnCommentBtn.setOnClickListener(returnCommentButtonListener);
		
		myUsernameView = (TextView)this.findViewById(R.id.my_username);
		myComment =  (EditText)this.findViewById(R.id.my_comment);
		
		Bundle bundle = this.getIntent().getExtras();
		topic =	bundle.getString("topic");
		postingId = bundle.getString("postingId");
		
		SharedPreferences sharedPreferences = getSharedPreferences("wonderful",
				Context.MODE_PRIVATE);
		username = sharedPreferences.getString("username", "");
		accountId = sharedPreferences.getString("accountid", "");
		myUsernameView.setText(username+" :");
	


		service  = new DotaDiscussionRestService(topic,this);
		

	}
	
	OnClickListener submitCommentButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			PostingDiscussion postingDiscussion = new PostingDiscussion();
              
			 String content = myComment.getText().toString();
			 if(content == null || "".equals(content.trim())){
				 Toast.makeText(SubmitPostingDiscussionActivity.this, "请输入评论内容!", 2).show();
				 return ;
			 }
			 if(content.length()>=1000){
				 Toast.makeText(SubmitPostingDiscussionActivity.this, "评论内容太长,超过最大限制!!", 2).show();
				 return ;
			 }

			 postingDiscussion.setContent(content);

			 postingDiscussion.setPostingId(Integer.parseInt(postingId));
			 
			 service.addDiscussion(postingDiscussion);
			 waitingDialog();
		}
	};
	
	
	OnClickListener returnCommentButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			SubmitPostingDiscussionActivity.this.finish();
		}
	};
	
	private ProgressDialog dialog;

	private void waitingDialog() {
		dialog = new ProgressDialog(this);
		dialog.setIcon(R.drawable.smile);
		dialog.setTitle("请稍等");
		dialog.setMessage("正在添加评论.....");
		dialog.setCancelable(true);
		dialog.show();

	}
	
	class DotaDiscussionRestService {
        private String topic ;
        private Context  context;
        
        public DotaDiscussionRestService(String topic, Context context){
        	this.topic = topic;
        	this.context = context;
        }

      
		 public  void  addDiscussion(final PostingDiscussion postingDiscussion){
	 			final RequestParams params = new RequestParams();
				 params.put("heroname", topic);
				 JSONObject object = new JSONObject();
				 try {
					 
					 object.put("content", postingDiscussion.getContent());
					 object.put("postingId", postingDiscussion.getPostingId());
					 StringEntity entity = new StringEntity(object.toString(), "UTF-8");

					 DotaDiscussionRestClient.post(SubmitPostingDiscussionActivity.this, "/dota/postingdiscussions.json?accountId="+accountId, entity, "application/json;charset=UTF-8", new JsonHttpResponseHandler() {
				            @Override
				            public void onSuccess(JSONObject object) {
				            	 if(object!=null ){
				            		   try {
										String id = object.getString("id");
										if(id !=null && id.length()>0){
											Toast.makeText(context, "添加评论成功!", 2).show();
							            		SubmitPostingDiscussionActivity.this.setResult(4, SubmitPostingDiscussionActivity.this.getIntent());
							            		SubmitPostingDiscussionActivity.this.finish();
										}else{
											 Toast.makeText(context, "添加评论失败!", 2).show();
										}
									} catch (JSONException e) {
									
										 Toast.makeText(context, "添加评论失败!", 2).show();
									}finally{
										dialog.dismiss();
									}
				            		   
				            	 }
				            	
				            }
				           public void onFailure(Throwable error){
				        	   dialog.dismiss();
				        	   Toast.makeText(context, "添加评论失败!", 2).show();
				           }
				            
				        });
 
				} catch (Exception e) {
					
					e.printStackTrace();
				}
				 
			 }	
	}
}
